package com.huangtang.pc.result;

import com.alibaba.fastjson2.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.huangtang.pc.enums.BaseEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
 * @author sunjf
 * @date 2024/01/05
 */
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "状态码")
    private Integer code;

    @ApiModelProperty(value = "返回信息")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    private T data;
    @JSONField(format = "yyyy-MM-dd HH:mm:ss:SSS")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss:SSS")
    @ApiModelProperty(value = "当前时间", position = 3)
    private Date date;

    public Result(T data, Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        this.date = new Date();
        this.data = data;
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        this.date = new Date();
    }


    public static <T> Result<T> success(T data) {
        return success(data, BaseEnum.SUCCESS.getCode(), BaseEnum.SUCCESS.getMsg());
    }

    public static <T> Result<T> success(T data, Integer code, String msg) {
        return new Result<T>(data, code, msg);
    }

    public static <T> Result<T> error(String msg) {
        return error(msg, BaseEnum.INTERNAL_SERVER_ERROR.getCode());
    }

    public static <T> Result<T> error(String msg, Integer code) {
        return new Result<T>(code, msg);
    }
}
